<template>
    <van-overlay :show="loading">
      <div class="loading">
        <van-loading style="z-index:999" color="#0094ff" />
      </div>
    </van-overlay>
  <div style="height: 50px;">
    <van-nav-bar ref="VanNavBarDmo" :title="title" left-text="返回" left-arrow @click-left="onClickLeft" fixed />
  </div>
  <van-address-list v-model="chosenAddressId" :list="list" default-tag-text="最高" :add-button-text="buttonText()"
    @add="onAdd" @edit="onEdit" @select="setSelect" />
</template>

<script>
import { useRoute, useRouter } from 'vue-router'
import { Base64 } from 'js-base64'
import { getEducationList, getTitleList, getSkillList, isMaxInfo } from '@/api/addressEdit.js'
import { useCounterStore } from '@/pinia/user.js'
import { Toast } from 'vant';



export default {
  setup() {
    // 获取pin
    const CounterStore = useCounterStore()
    const Route = useRoute()
    const Router = useRouter()
    const query = JSON.parse(Base64.decode(Route.query.data))
    const loading = ref(false)
    const title = ref(query.name)
    function buttonText() {
      const key = query.key
      return key === 'education' ? '添加学历' : key === 'theTitleTechnicalPost' ? '添加职称信息' : key === 'skill' ? '添加技能信息' : ''
    }
    function onClickLeft() { history.back() }
    const chosenAddressId = ref();

    const list = reactive([])
    const defaultList = reactive([])
    getList()
    async function getList() {
      loading.value = true
      list.length = 0
      defaultList.length = 0
      const id = CounterStore.employeeId
      let res
      let arr = []
      if (query.key === 'education') res = await getEducationList(id)
      if (query.key === 'theTitleTechnicalPost') res = await getTitleList(id)
      if (query.key === 'skill') res = await getSkillList(id)
      if (res.success) {
        arr = res.message
      } else {
        Toast.fail({
          message: res.message,
          forbidClick: true,
          duration: 1000
        })
      }
      arr.forEach(item => {
        defaultList.push(item)
        const a = setList(item)
        list.push(a)
      })
      loading.value = false
    }

    function setList(item) {
      let a
      if (query.key === 'theTitleTechnicalPost') {
        a = {
          id: item.titleId,
          name: `职称名称：${item.titleName}`,
          tel: `职称等级：${item.titleGrade}`,
          address: `获取方式：${item.acquisitionType}`,
          isDefault: false,
        }
        if (item.isTitleMax === '是') { chosenAddressId.value = item.titleId; a.isDefault = true }
      }
      if (query.key === 'education') {
        a = {
          id: item.educationId,
          name: `${item.schoolName}`,
          tel: `${item.trainType},${item.schoolType},${item.educationType}`,
          address: `${item.enrollmentStartDate} 至 ${item.enrollmentEndDate}`,
        }

        if (item.isMaxEducation === '是') { chosenAddressId.value = item.educationId; a.isDefault = true }
      }
      if (query.key === 'skill') {
        a = {
          id: item.skillId,
          name: `工种：${item.workType}`,
          tel: `技术等级：${item.skillGrade}`,
          address: `获取方式：${item.skillAcquisitionType} , 获取时间：${item.skillAcquisitionDate}`,
          isDefault: false,
        }
        if (item.isSkillMax === '是') { chosenAddressId.value = item.skillId; a.isDefault = true }
      }
      return a
    }


    const onAdd = () => {

      const obj = { key: query.key }
      if (query.key === 'theTitleTechnicalPost') obj.name = "添加职称信息"
      if (query.key === 'education') obj.name = '添加学历信息'
      if (query.key === 'skill') obj.name = '添加技能信息'
      if (+CounterStore.isSubmit === 1) return Toast(`当前信息已提交暂不支持${obj.name}`);
      const data = Base64.encode(JSON.stringify(obj))
      Router.push({
        path: '/AddressEdit',
        query: { data }
      })
    }
    const onEdit = (item, index) => {
      let defaultItem = {}
      let idName
      let type
      if (query.key === 'education') { idName = 'educationId'; type = 1 }
      if (query.key === 'theTitleTechnicalPost') { idName = 'titleId'; type = 3 }
      if (query.key === 'skill') { idName = 'skillId'; type = 2 }
      defaultList.forEach(it => { if (item.id === it[idName]) defaultItem = it })
      const obj = { id: item.id, index, key: query.key, type: type }
      if (query.key === 'theTitleTechnicalPost') obj.name = "编辑职称信息"
      if (query.key === 'education') obj.name = '编辑学历信息'
      if (query.key === 'skill') obj.name = '编辑技能信息'
      const data = Base64.encode(JSON.stringify(obj))
      Router.push({
        path: '/AddressEdit',
        query: { data }
      })
    }

    const setSelect = async (item) => {
      Toast.loading({
        message: '修改中....',
        forbidClick: true,
      })
      // isMaxInfo   ?employeeId=1&otherId=1&type=1
      const employeeId = CounterStore.employeeId
      const otherId = item.id
      const type = query.key === 'skill' ? 2 : query.key === 'education' ? 1 : query.key === 'theTitleTechnicalPost' ? 3 : ''
      const data = { employeeId, otherId, type }
      const res = await isMaxInfo(data)
      if (res.success) {
        Toast.success(res.message)
        getList()
      } else {
        Toast.fail(res.message);
      }

    }

    return {
      loading,
      list,
      onAdd,
      onEdit,
      chosenAddressId,
      title,
      onClickLeft,
      buttonText,
      setSelect
    };
  },
};
</script>
<style lang="scss">
.van-button--danger {
  background-color: var(--van-button-primary-background-color);
  border: 0px;
}

.van-tag--danger {
  background: var(--van-button-warning-border-color);
}

.van-address-item .van-radio__icon--checked .van-icon {
  background-color: var(--van-button-primary-background-color);
  border-color: var(--van-button-primary-background-color)
}
.van-overlay{
  z-index:99999
}
.loading {
  margin-top: 70%;
}
</style>
